<?php

namespace App\Http\Controllers\Api\V1\Frontend\Index;

use Illuminate\Http\Request;
use App\Services\AuthService;
use App\Services\BaseService;
use App\Services\IndexService;
use App\Services\ProcessService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;

class IndexController extends BaseService
{
    /**
     * 首页展示
     * @param IndexService $indexService
     * @param AuthService $authService
     * @param ProcessService $processService
     * @return mixed
     */
    public function index(IndexService $indexService,AuthService $authService,ProcessService $processService)
    {
        $userInfo = $authService->user();

        if ($userInfo['status'] == 'success') {
            $user = [
                'name'       => $userInfo['data']['user']['name'],
                'department' => $userInfo['data']['user']['department']
            ];
        } else {
            $user = [];
        }

        $numbers = [
            'need_deal_number'     => $processService->getNeedDealNumber(),
            'need_read_number'     => $processService->getNeedReadNumber(),
            'join_number'          => $processService->getJoinNumber(),
            'finish_number'        => $processService->getFinishNumber(),
            'my_process_number'    => $processService->getMyProcessNumber(),
            'group_process_number' => $processService->getMyGroupProcessNumber()
        ];

        $report_manage = $indexService->reportManage();

        $tables = $indexService->getTables();

        $data = compact('user','numbers','report_manage','tables');

        return $this->successful($data,'success');
    }

    /**
     * 搜索
     * @param Request $request
     * @param IndexService $indexService
     * @return mixed
     * @throws ValidationException
     */
    public function search(Request $request,IndexService $indexService)
    {
        Validator::make($request->all(),[
            'search' => 'nullable|string',
            'type'   => 'nullable|int:1,2,3,4,5'
        ])->validate();

        if (!$request->filled('type')) {
            $request->offsetSet('type',1);
        }

        $data = $indexService->search($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 服务流程列表数据
     * @param Request $request
     * @param ProcessService $processService
     * @return mixed
     * @throws ValidationException
     */
    public function serverList(Request $request,ProcessService $processService)
    {
        Validator::make($request->all(),[
            'date '  => 'nullable|string',
            'type'   => 'required|int:1,2,3,4,5,6'
        ])->validate();

        $data = $processService->getServiceList($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }
}
